package com.xxxy.ljh.myapplication.activity.login;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.material.textfield.TextInputEditText;
import com.xxxy.ljh.myapplication.DaoImpl.UserDaoImpl;
import com.xxxy.ljh.myapplication.R;
import com.xxxy.ljh.myapplication.model.User;

public class RegisterActivity extends AppCompatActivity {
    private TextInputEditText etUsername, etEmail, etPassword, etStudentId, etDepartment, etRealName, etPhone;
    private Button btnRegister, btnBackToLogin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initViews();
    }

    private void initViews() {
        etUsername = findViewById(R.id.etUsername);
        etEmail = findViewById(R.id.etEmail);
        etPassword = findViewById(R.id.etPassword);
        etStudentId = findViewById(R.id.etStudentId);
        etRealName = findViewById(R.id.etRealName);
        etPhone = findViewById(R.id.etPhone);
        etDepartment = findViewById(R.id.etDepartment);
        btnRegister = findViewById(R.id.btnRegister);
        btnBackToLogin = findViewById(R.id.btnBackToLogin);

        btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                performRegister();
            }
        });

        btnBackToLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void performRegister() {
        String username = etUsername.getText().toString().trim();
        String email = etEmail.getText().toString().trim();
        String password = etPassword.getText().toString().trim();
        String studentId = etStudentId.getText().toString().trim();
        String realName = etRealName.getText().toString().trim();
        String phone = etPhone.getText().toString().trim();
        String department = etDepartment.getText().toString().trim();
        UserDaoImpl userDao = new UserDaoImpl(this);

        // 输入验证
        if (username.isEmpty() || email.isEmpty() || password.isEmpty() ||
                studentId.isEmpty() || realName.isEmpty() || phone.isEmpty() || department.isEmpty()) {
            Toast.makeText(this, "请填写所有信息", Toast.LENGTH_SHORT).show();
            return;
        }

        if (username.length() < 3) {
            Toast.makeText(this, "用户名长度至少3位", Toast.LENGTH_SHORT).show();
            return;
        }

        if (!android.util.Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
            Toast.makeText(this, "邮箱格式错误", Toast.LENGTH_SHORT).show();
            return;
        }

        if (!phone.matches("^1[3-9]\\d{9}$")) {
            Toast.makeText(this, "手机号格式错误", Toast.LENGTH_SHORT).show();
            return;
        }

        if (!studentId.matches("^[0-9]{6,12}$")) {
            Toast.makeText(this, "学号格式错误", Toast.LENGTH_SHORT).show();
            return;
        }

        if (realName.length() < 2) {
            Toast.makeText(this, "真实姓名至少2个字符", Toast.LENGTH_SHORT).show();
            return;
        }

        if (department.length() < 2) {
            Toast.makeText(this, "部门名称至少2个字符", Toast.LENGTH_SHORT).show();
            return;
        }

        if (userDao.checkName(username)) {
            Toast.makeText(this, "用户名已存在", Toast.LENGTH_SHORT).show();
            return;
        }

        if (password.length() < 6) {
            Toast.makeText(this, "密码长度至少6位", Toast.LENGTH_SHORT).show();
            return;
        }

        // 创建 User 对象
        User user = new User(username, password, realName, phone, email, studentId, department);

        // 注册逻辑
        if (userDao.register(user)) {
            Toast.makeText(this, "注册成功", Toast.LENGTH_SHORT).show();
            finish();
        } else {
            Toast.makeText(this, "注册失败", Toast.LENGTH_SHORT).show();
        }
    }
}
